/*
 * Copyright (C) 2020 FoilPlanet
 *
 */

#ifndef _ANBOX_CODEC_VIDEO_SINK_H_
#define _ANBOX_CODEC_VIDEO_SINK_H_

#include "anbox/video/sink.h"
#include "anbox/graphics/buffer_queue.h"
#include "anbox/network/socket_messenger.h"
#include "anbox/network/connector.h"
#include <vector>

namespace anbox {
namespace codec {

/**
 * \ref hal_h264e_stub.h: RVPU_PRIM_CODE
 * message format: {code}:{length}:{data}
 */
enum MppRvpuPrimCode {
    RVPU_PRIM_UNDEFINED = 0,
    RVPU_PRIM_INIT,
    RVPU_PRIM_DEINIT,
    RVPU_PRIM_REGS,
    RVPU_PRIM_START,
    RVPU_PRIM_WAIT,
    RVPU_PRIM_CONTROL_PREP,
    RVPU_PRIM_CONTROL_RC,
    RVPU_PRIM_CONTROL_CODEC,
    RVPU_PRIM_CONTROL_SEI,
    RVPU_PRIM_CONTROL,
    RVPU_PRIM_RESET,
    RVPU_PRIM_FLUSH,
    RVPU_PRIM_MAX
};

class VideoSink : public video::Sink {
  public:
    VideoSink(const std::shared_ptr<anbox::Runtime> &runtime);

    virtual ~VideoSink();

    void process_data(const uint8_t *data, size_t size) override;

  protected:
    void stream_init(void *cfg);
    void stream_control(MppRvpuPrimCode prim, void *cfg);
    void stream_reset();
    void stream_flush();
    void stream_end();

    void encode_packet(void *data, size_t size);
    void wait_packet(void *data, size_t size);

  private:
    static std::shared_ptr<network::Connector> m_serv;
    static std::vector<std::shared_ptr<network::SocketMessenger>> m_clients;
};
} // namespace codec
} // namespace anbox

#endif /* _ANBOX_CODEC_VIDEO_SINK_H_ */
